/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package com.opensymphony.user.provider.hibernate3.ahxu.impl;




import ahxu.workflow.ofbiz2hibernate.hibernate3.DefaultHibernateConfigurationProvider;
import ahxu.workflow.ofbiz2hibernate.hibernate3.PropertyBoolean;
import ahxu.workflow.ofbiz2hibernate.hibernate3.PropertyDate;
import ahxu.workflow.ofbiz2hibernate.hibernate3.PropertyDouble;
import ahxu.workflow.ofbiz2hibernate.hibernate3.PropertyNumber;
import ahxu.workflow.ofbiz2hibernate.hibernate3.PropertySetItemImpl;
import ahxu.workflow.ofbiz2hibernate.hibernate3.PropertyString;
import com.opensymphony.user.provider.hibernate3.ahxu.OSUserHibernateConfigurationProvider;
import com.opensymphony.user.provider.hibernate3.ahxu.dao.HibernateGroupDAO;
import com.opensymphony.user.provider.hibernate3.ahxu.dao.HibernateUserDAO;
import com.opensymphony.user.provider.hibernate3.ahxu.dao.SessionManager;

import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.cfg.Configuration;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Iterator;
import java.util.Map;
import org.hibernate.SessionFactory;


/**
 * A OSUser provider that is backed by Hibernate.
 */
public class OSUserHibernateConfigurationProviderImpl extends DefaultHibernateConfigurationProvider implements OSUserHibernateConfigurationProvider {
    //~ Static fields/initializers /////////////////////////////////////////////

    protected static Log log = LogFactory.getLog(OSUserHibernateConfigurationProviderImpl.class.getName());

    //~ Instance fields ////////////////////////////////////////////////////////

    private Configuration configuration;
    private HibernateGroupDAO groupDAO;
    private HibernateUserDAO userDAO;
    private SessionManager sessionManager;

    //~ Constructors ///////////////////////////////////////////////////////////

    public OSUserHibernateConfigurationProviderImpl() {
    }


    //~ Methods ////////////////////////////////////////////////////////////////
    //ahxu
    public void init(){
        this.sessionManager=new SessionManager(this.getSessionFactory());
    }
    public Configuration getConfiguration() {
        return configuration;
    }

    /**
     * Return the Group DAO
     */
    public HibernateGroupDAO getGroupDAO() {
        if (groupDAO == null) {
            groupDAO = new HibernateGroupDAOImpl(sessionManager);
        }

        return groupDAO;
    }

    /**
     * Return the User DAO.
     */
    public HibernateUserDAO getUserDAO() {
        if (userDAO == null) {
            userDAO = new HibernateUserDAOImpl(sessionManager);
        }

        return userDAO;
    }

    /**
     * Pass in the configuration values required to create a new
     * SessionManager.
     */
    public void setupConfiguration(Map configurationProperties) {
        if (configuration == null) {
            configuration = new Configuration();

            try {
                //configuration.buildSessionFactory().
                configuration.addClass(HibernateGroupImpl.class);
                configuration.addClass(HibernateUserImpl.class);
                configuration.addClass(PropertySetItemImpl.class);
                configuration.addClass(PropertyString.class);
                configuration.addClass(PropertyNumber.class);
                configuration.addClass(PropertyBoolean.class);
                configuration.addClass(PropertyDouble.class);
                configuration.addClass(PropertyDate.class);

                if (configurationProperties != null) {
                    Iterator itr = configurationProperties.keySet().iterator();

                    while (itr.hasNext()) {
                        String key = (String) itr.next();

                        if (key.startsWith("hibernate")) {
                            configuration.setProperty(key, (String) configurationProperties.get(key));
                        }
                    }
                }

                sessionManager = new SessionManager(configuration);
                super.setSessionFactory(sessionManager.getSessionFactory());
            } catch (MappingException e) {
                log.error("Could not create new Hibernate configuration: " + e, e);
            } catch (HibernateException e) {
                log.error("Could not create new Hibernate configuration: " + e, e);
            }
        }
    }
}
